package com.aso.common.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;

import com.aso.common.R;
import com.itsdf07.alog.ALog;

/**
 * Created by itsdf07 on 2017/7/9 11:22.
 * E-Mail: 923255742@qq.com
 * GitHub: https://github.com/itsdf07
 */

public class AlertDialogCustomConfirm {

    public static class Builder {
        private Context context;
        /**
         * AlertDialog Icon
         */
        private int iconId = android.R.drawable.ic_dialog_info;

        /**
         * AlertDialog title
         */
        private String titleTip;

        /**
         * AlertDialog content
         */
        private String contentTip;

        /**
         * PositiveButtonTip：确认按钮
         */
        private String positiveButtonTip;
        /**
         * NegativeButtonTip：取消按钮
         */
        private String negativeButtonTip;

        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * set AlertDialot title
         *
         * @param titleTip
         * @return
         */
        public Builder setTitle(String titleTip) {
            this.titleTip = titleTip;
            return this;
        }

        /**
         * set AlertDialog Icon
         *
         * @param iconId
         * @return
         */
        public Builder setIcon(int iconId) {
            this.iconId = iconId;
            return this;
        }

        /**
         * set AlertDialog content
         *
         * @return
         */
        public Builder setContent(String contentTip) {
            this.contentTip = contentTip;
            return this;
        }

        /**
         * Set the positive button String and it's listener
         *
         * @param negativeButtonTip
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String negativeButtonTip,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonTip = negativeButtonTip;
            positiveButtonClickListener = listener;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonTip
         * @param listener
         * @return
         */
        public Builder setNegativeButton(String positiveButtonTip,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonTip = positiveButtonTip;
            negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Set the positive button String and it's listener
         *
         * @param listener
         * @return
         */
        public Builder setPositiveButton(DialogInterface.OnClickListener listener) {
            return setPositiveButton("", listener);
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param listener
         * @return
         */
        public Builder setNegativeButton(DialogInterface.OnClickListener listener) {
            return setNegativeButton("", listener);
        }

        /**
         * create the DialogCustom to show
         *
         * @return DialogCustom
         */
        public AlertDialog create() {
            if (null == context) {
            }
            initTip();
            return new AlertDialog.Builder(context)
                    .setTitle(titleTip)
                    .setIcon(iconId)
                    .setMessage(contentTip)
                    .setPositiveButton(positiveButtonTip, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (null == positiveButtonClickListener) {
                                ALog.e("positiveButtonClickListener is null");
                                return;
                            }
                            positiveButtonClickListener.onClick(dialog, which);
                        }
                    })
                    .setNegativeButton(negativeButtonTip, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (null == negativeButtonClickListener) {
                                ALog.e("negativeButtonClickListener is null");
                                return;
                            }
                            negativeButtonClickListener.onClick(dialog, which);
                        }
                    }).show();
        }

        /**
         * 初始化tip相关
         *
         * @return
         */
        private void initTip() {
            if (TextUtils.isEmpty(titleTip)) {
                titleTip = "";
            }
            if (TextUtils.isEmpty(contentTip)) {
                contentTip = "";
            }
            if (TextUtils.isEmpty(positiveButtonTip)) {
                positiveButtonTip = context.getString(R.string.confirm);
            }
            if (TextUtils.isEmpty(negativeButtonTip)) {
                negativeButtonTip = context.getString(R.string.cancle);
            }
        }


    }
}
